package com.yindong.camera.util;

import ohos.app.AbilityContext;
import ohos.bundle.IBundleManager;

/**
 * 权限操作工具类
 *
 * @author      殷冬
 * @date        2021-01-22
 * @version     1.0.0
 */
public class PermissionUtil {

    private PermissionUtil(){}

    /**
     * 权限申请方法
     *
     * @param context           上下文
     * @param permissions       需要申请的权限列表
     */
    /*public static void permissionApply(AbilityContext context, String ... permissions){
        // 存储未授权的权限信息
        String[] unauthorizedInfo = new String[permissions.length];
        for(Integer i = 0; i<permissions.length; i++){
            // 判断是否已授权
            if(context.canRequestPermission(permissions[i])){
                unauthorizedInfo[i] = permissions[i];
            }
        }
        // 非空进行授权
        if(! EmptyUtil.isEmpty(unauthorizedInfo)){
            context.requestPermissionsFromUser(unauthorizedInfo, 0);
        }
    }*/

    public static void permissionApply(AbilityContext context, Integer requestCode, String ... permissions){
        // 存储未授权的权限信息
        String[] unauthorizedInfo = new String[permissions.length];
        for(Integer i = 0; i<permissions.length; i++){
            if(context.verifySelfPermission(permissions[i]) != IBundleManager.PERMISSION_GRANTED){
                if(context.canRequestPermission(permissions[i])){
                    unauthorizedInfo[i] = permissions[i];
                }
            }
        }
        // 非空进行授权
        if(! EmptyUtil.isEmpty(unauthorizedInfo)){
            context.requestPermissionsFromUser(unauthorizedInfo, requestCode);
        }
    }

}
